package cn.com.code.design.group;

import cn.com.code.design.HttpClient;
import cn.com.code.design.NetMall;
import com.alibaba.fastjson.JSON;
import sun.misc.BASE64Encoder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhengpei
 * @date 2021/11/4
 */
public class JDNetMall extends NetMall {

  public JDNetMall(String uId, String pwd) {
    super(uId, pwd);
  }

  @Override
  protected Boolean login(String uId, String uPwd) {
    logger.info("模拟京东用户登录 uId:[{}],uPwd:[{}]", uId, uPwd);
    return true;
  }

  @Override
  protected Map<String, String> reptile(String skuUrl) {
    String str = HttpClient.doGet(skuUrl);
    Pattern pattern = Pattern.compile("(?<=title\\>).*(?=</title)");
    Matcher m9 = pattern.matcher(str);
    Map<String, String> map = new ConcurrentHashMap<String, String>();
    if (m9.find()) {
      map.put("name", m9.group());
    }
    map.put("price", "5999.00");
    logger.info("模拟京东商品爬虫解析：{} | {} 元 {}", map.get("name"), map.get("price"), skuUrl);
    return map;
  }

  @Override
  protected String createBase64(Map<String, String> goodsInfo) {
    BASE64Encoder encoder = new BASE64Encoder();
    logger.info("模拟生成京东商品base64海报");
    return encoder.encode(JSON.toJSONString(goodsInfo).getBytes());
  }

}
